package titt;

import titt.head.HTTPHead;
import titt.service.TITTService;
import titt.service.invoke.StandardMethodParser;
import titt.service.invoke.UnknownMethodRedirect;
import titt.service.proxy.NuderInvoker;
import titt.service.proxy.TITTProxyGenerator;
import titt.service.strategy.NuderStrategy;

public class TITTNuder {
	
	private static NuderStrategy strat = new NuderStrategy();
	private static TITTService nuder = new TITTService(strat);
	private static TITTProxyGenerator gen = new TITTProxyGenerator();

	
	public static HTTPHead getNuderHead() {
		return strat.getPackage(Thread.currentThread().getId()).getHead();
	}
	
	public static void register(Object o, String... tokens) {
		nuder.register(o, new UnknownMethodRedirect("get", new StandardMethodParser()), tokens);
	}
	
	public static void unregister(Object o) {
		nuder.unregister(o);
	}
	
	public static <T> T createProxy(String t0, T[]... empty) {
		return createProxy(getType(empty), t0);
	}
	
	public static <T> T createProxy(String t0, String t1, T[]... empty) {
		return createProxy(getType(empty), t0, t1);
	}
	
	public static <T> T createProxy(String t0, String t1, String t2, T[]... empty) {
		return createProxy(getType(empty), t0, t1, t2);
	}

	public static <T> T createProxy(String t0, String t1, String t2, String t3, T[]... empty) {
		return createProxy(getType(empty), t0, t1, t2, t3);
	}

	public static <T> T createProxy(String t0, String t1, String t2, String t3, String t4, T[]... empty) {
		return createProxy(getType(empty), t0, t1, t2, t3, t4);
	}

	public static <T> T createProxy(String t0, String t1, String t2, String t3, String t4, String t5, T[]... empty) {
		return createProxy(getType(empty), t0, t1, t2, t3, t4, t5);
	}

	public static <T> T createProxy(String t0, String t1, String t2, String t3, String t4, String t5, String t6, T[]... empty) {
		return createProxy(getType(empty), t0, t1, t2, t3, t4, t5, t6);
	}

	public static <T> T createProxy(String t0, String t1, String t2, String t3, String t4, String t5, String t6, String t7, T[]... empty) {
		return createProxy(getType(empty), t0, t1, t2, t3, t4, t5, t6, t7);
	}

	public static <T> T createProxy(Class<T> c, String... tokens) {
		try {
			
			return gen.createGenericTITTProxy(c, new NuderInvoker(tokens));

		}
		catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	private static <T> Class<T> getType(T[]... t) {
		return (Class<T>)t.getClass().getComponentType().getComponentType();
	}


}
